//
//  FoucesViewController.m
//  XiangMuB
//
//  Created by 向修根 on 16/8/12.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "FoucesViewController.h"
#import "TopicTableViewCell.h"
#import "TopicImageTableViewCell.h"
#import "DataBaseHandle.h"
#import "TopocModel.h"
#import "TopicDetailsViewController.h"
#import "MBProgressHUD.h"
#import <MJRefresh.h>

@interface FoucesViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *foucesTableView;
@property (strong,nonatomic) NSMutableArray *dataArray;

@property (strong,nonatomic) TopicTableViewCell *cell;

@property (strong,nonatomic) TopicImageTableViewCell *imageCell;

@property (strong,nonatomic) NSString *str;


@end

@implementation FoucesViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
  
    //    下拉加载
    self.foucesTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArray removeAllObjects];
        [self takeOut];
    }];
    [self.foucesTableView.mj_header beginRefreshing];
    [self.foucesTableView reloadData];
}


- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //夜间模式
    [self.foucesTableView NightWithType:(UIViewColorTypeNormal)];
  
    self.foucesTableView.delegate = self;
    self.foucesTableView.dataSource = self;
    
    [self.foucesTableView registerNib:[UINib nibWithNibName:@"TopicTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    [self.foucesTableView registerNib:[UINib nibWithNibName:@"TopicImageTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cellImage"];
//    [self takeOut];

   
 
    
    // Do any additional setup after loading the view from its nib.
}

- (void)takeOut{
    [[DataBaseHandle shareDataBaseHandle]openDatabase];
    NSMutableArray *array = [[DataBaseHandle shareDataBaseHandle]select];
    for (NSDictionary *dic in array) {
        TopocModel *model = [TopocModel new];
        model.name = dic[@"name"];
        model.classification = dic[@"classification"];
        model.concernCount = dic[@"concernCount"];
        model.talkCount = dic[@"talkCount"];
        model.subjectId = dic[@"subjectId"];
        
        NSMutableArray *array = [NSMutableArray array];
        NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        NSString *title1 = dic[@"OneHead"];
        NSString *title2 = dic[@"twoHeader"];
        NSString *titleImage1 = dic[@"OneHeadImage"];
        NSString *titleImage2 = dic[@"twoHeadImage"];
        [dic1 setValue:title1 forKey:@"content"];
        [dic1 setValue:titleImage1 forKey:@"userHeadPicUrl"];
        [dict setValue:title2 forKey:@"content"];
        [dict setValue:titleImage2 forKey:@"userHeadPicUrl"];
        [array addObject:dic1];
        [array addObject:dict];
        model.talkContent = array;

        
        NSString *image1 = dic[@"image1"];
        NSString *image2 = dic[@"image2"];
        NSString *image3 = dic[@"image3"];
        NSMutableArray *array1 = [NSMutableArray array];
        if (image1 != nil) {
            [array1 addObject:image1];
            [array1 addObject:image2];
            [array1 addObject:image3];
            model.talkPicture = array1;
        }
        

         [self.dataArray addObject:model];
        
  
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.foucesTableView.mj_header endRefreshing];
                [self.foucesTableView reloadData];
                
            });
   
        
        

        
        
        
        
    }
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    TopocModel *model = self.dataArray[indexPath.row];
    self.str = [NSString stringWithString:model.name];
  
    if (model.talkPicture == nil) {
        self.cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        //夜间模式
        [self.cell NightWithType:(UIViewColorTypeNormal)];
        
        [self.cell.cellView NightWithType:UIViewColorTypeClear];
        
        
        //        点击cell不变色
       self.cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if ([_cell.name.text isEqualToString:model.name]) {
            
        }else{
             _cell.model = model;
        }
        
        [self.cell.fouceButton addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
        
        return self.cell;

    }else{
        
        self.imageCell = [tableView dequeueReusableCellWithIdentifier:@"cellImage" forIndexPath:indexPath];
        
        //夜间模式
        [self.imageCell NightWithType:(UIViewColorTypeNormal)];
        
        
         [self.imageCell.cellView NightWithType:UIViewColorTypeClear];
        
        //        点击cell不变色
        self.imageCell.selectionStyle = UITableViewCellSelectionStyleNone;
   
        if (self.imageCell.name.text != model.name) {
            self.imageCell.model = model;
        }
//             self.imageCell.model = model;

        

        return self.imageCell;
        
    }
    
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    self.foucesTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self takeOut];
    }];
    [self.foucesTableView.mj_header beginRefreshing];
    [self.foucesTableView reloadData];
    [self.cell.fouceButton removeObserver:self forKeyPath:@"name"];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    TopocModel *model = self.dataArray[indexPath.row];
    if (model.talkPicture == nil) {
        return 200;
    }else{
        return 220;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TopocModel *model = self.dataArray[indexPath.row];
    TopicDetailsViewController *topic = [TopicDetailsViewController new];
    topic.strID = model.subjectId;
    [self showViewController:topic sender:nil];
    

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
